package com.workcell.workdata.xthings.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.excel.annotation.ResponseExcel;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseSmartTanks;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.service.BaseSmartTanksService;
import com.workcell.workdata.xthings.service.ManufacturerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/baseSmartTanks" )
@Api(value = "baseSmartTanks", tags = "智能罐体表管理")
public class BaseSmartTanksController {

    private final BaseSmartTanksService baseSmartTanksService;
    private final ManufacturerService manufacturerService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param baseSmartTanks 上位机表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getBaseSmartTanksPage(Page page, BaseSmartTanks baseSmartTanks) {
        return R.ok(baseSmartTanksService.page(page, Wrappers.query(baseSmartTanks)));
    }

    /**
     * 模糊分页查询
     * @param baseVo
     * @return
     */
    @ApiOperation(value = "模拟分页查询", notes = "模拟分页查询")
    @PostMapping("/vaguePage" )
    public R vaguePage(@RequestBody BaseVo baseVo) {
        return R.ok(baseSmartTanksService.vaguePage(baseVo));
    }

    /**
     * 通过id查询上位机表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) String id) {
        return R.ok(baseSmartTanksService.getById(id));
    }

    /**
     * 新增上位机表
     * @param baseSmartTanks 上位机表
     * @return R
     */
    @ApiOperation(value = "新增上位机表", notes = "新增上位机表")
    @PostMapping
    public R save(@RequestBody BaseSmartTanks baseSmartTanks) {
		Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseSmartTanks.getDeviceManufacturer());
		Assert.notNull(manufacturer, "厂商查询为空");
		baseSmartTanks.setManufacturerShortName(manufacturer.getManufacturerShortName());
		baseSmartTanks.setManufacturerFullName(manufacturer.getManufacturerFullName());
		baseSmartTanks.setManufacturer(manufacturer.getManufacturerFullName());
		baseSmartTanks.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(baseSmartTanksService.save(baseSmartTanks));
    }

    /**
     * 修改上位机表
     * @param baseSmartTanks 上位机表
     * @return R
     */
    @ApiOperation(value = "修改上位机表", notes = "修改上位机表")
    @PutMapping
    public R updateById(@RequestBody BaseSmartTanks baseSmartTanks) {
        Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseSmartTanks.getDeviceManufacturer());
        Assert.notNull(manufacturer, "厂商查询为空");
        baseSmartTanks.setManufacturerShortName(manufacturer.getManufacturerShortName());
        baseSmartTanks.setManufacturerFullName(manufacturer.getManufacturerFullName());
        baseSmartTanks.setManufacturer(manufacturer.getManufacturerFullName());
		baseSmartTanks.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(baseSmartTanksService.updateById(baseSmartTanks));
    }

    /**
     * 通过id删除上位机表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除上位机表", notes = "通过id删除上位机表")
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable String id) {
        return R.ok(baseSmartTanksService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param baseSmartTanks 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<BaseSmartTanks> export(BaseSmartTanks baseSmartTanks) {
        return baseSmartTanksService.list(Wrappers.query(baseSmartTanks));
    }

    /**
     * 修改状态
     * @param baseSmartTanks
     * @return R
     */
    @ApiOperation(value = "修改上位机状态信息", notes = "修改上位机状态信息")
    @PutMapping("/updateStateById")
    public R updateStateById(@RequestBody BaseSmartTanks baseSmartTanks) {
        return R.ok(baseSmartTanksService.updateStateById(baseSmartTanks));
    }

}
